﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web.Mvc;
using ControleRepresentante.Logic.Models;
using ControleRepresentante.Logic.Repositories;
using ControleRepresentante.Logic.Statistics;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using WebSite.Controllers;
using WebSite.ViewModels.EditViewModels;

namespace ControleRepresentante.UnitTests
{
    public static class TestScenarioRelatorioVisitaHelper
    {
        public static void DeleteRelatorioVisita(this TestScenario testScenario, Guid relatorioVisitaId)
        {
            IRelatorioVisitaRepository relatorioVisitaRepository = testScenario.RepositoryProvider.GetRepository<IRelatorioVisitaRepository>();
            StatisticsBase statistics;
            int countBefore = relatorioVisitaRepository.GetModels(out statistics).Count();
            using (RelatorioVisitaController relatorioVisitaController = new RelatorioVisitaController(testScenario.RepositoryProvider, testScenario.SessionConfiguration, testScenario.ControllerExtensionProvider))
            {
                relatorioVisitaController.Delete(relatorioVisitaId);
                relatorioVisitaController.Delete(relatorioVisitaId, null);
                Assert.AreEqual(countBefore - 1, relatorioVisitaRepository.GetModels(out statistics).Count());
            }
        }

        public static Guid CreateRelatorioVisita(this TestScenario testScenario, RelatorioVisita relatorioVisita)
        {
            IRelatorioVisitaRepository relatorioVisitaRepository = testScenario.RepositoryProvider.GetRepository<IRelatorioVisitaRepository>();
            StatisticsBase statistics;
            int countBefore = relatorioVisitaRepository.GetModels(out statistics).Count();
            using (RelatorioVisitaController relatorioVisitaController = new RelatorioVisitaController(testScenario.RepositoryProvider, testScenario.SessionConfiguration, testScenario.ControllerExtensionProvider))
            {
                ActionResult actionResult = relatorioVisitaController.Create();
                ViewResult viewResult = (ViewResult)actionResult;
                Assert.IsInstanceOfType(viewResult.Model, typeof(RelatorioVisitaEditViewModel));
                RelatorioVisitaEditViewModel relatorioVisitaEditViewModel = (RelatorioVisitaEditViewModel)viewResult.Model;
                Assert.IsNull(relatorioVisitaEditViewModel.Message);

                NameValueCollection collection = new NameValueCollection();
                if (relatorioVisita.RelatorioVisitaJolitex != null)
                {
                    collection.Add("ocorrencia", relatorioVisita.RelatorioVisitaJolitex.Ocorrencia.ToString());
                }
                foreach (AttributeValue attributeValue in relatorioVisita.AttributeValues)
                {
                    collection.Add("_" + attributeValue.Attribute.Name, attributeValue.Value);
                }
                FormCollection formCollection = new FormCollection(collection);
                actionResult = relatorioVisitaController.Create(relatorioVisita.Data, relatorioVisita.Cliente.Id, relatorioVisita.Vendedor.Id, relatorioVisita.Observacao, formCollection);
                IEnumerable<RelatorioVisita> relatorioVisitas = relatorioVisitaRepository.GetModels(out statistics);
                Assert.AreEqual(countBefore + 1, relatorioVisitas.Count());
                RelatorioVisita addedRelatorioVisita = relatorioVisitas.FirstOrDefault(x => x.Observacao == relatorioVisita.Observacao);
                Assert.AreEqual(relatorioVisita.Cliente.Id, addedRelatorioVisita.Cliente.Id);
                Assert.AreEqual(relatorioVisita.Data, addedRelatorioVisita.Data);
                Assert.AreEqual(relatorioVisita.Observacao, addedRelatorioVisita.Observacao);
                Assert.AreEqual(relatorioVisita.Vendedor.Id, addedRelatorioVisita.Vendedor.Id);

                Assert.IsInstanceOfType(actionResult, typeof(RedirectToRouteResult));
                RedirectToRouteResult redirectToRouteResult = (RedirectToRouteResult)actionResult;
                Assert.AreEqual("Details", redirectToRouteResult.RouteValues["action"]);
                Assert.AreEqual(addedRelatorioVisita.Id, redirectToRouteResult.RouteValues["id"]);
                return addedRelatorioVisita.Id;
            }
        }

        internal static Guid CreateRelatorioVisita(this TestScenario testscenario, DateTime data, Guid vendedorId, Guid clienteId, string observacao, RelatorioVisitaJolitexOcorrencia ocorrencia)
        {
            RelatorioVisita relatorioVisita = new RelatorioVisita
            {
                Data = data,
                Observacao = observacao,
                Vendedor = new Vendedor {Id = vendedorId},
                Cliente = new Cliente {Id = clienteId}
            };
            relatorioVisita.SetRelatorioVisitaJolitex();
            relatorioVisita.RelatorioVisitaJolitex.Ocorrencia = ocorrencia;
            return testscenario.CreateRelatorioVisita(relatorioVisita);
        }
    }
}
